// 配置路由的地方
import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store'
// 使用插件
Vue.use(Router);
// 引入路由组件
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import GameNews from '@/pages/GameNews/GameNewsList'
import GameNewsDetail from '@/pages/GameNews/GameNewsDetail'
import GameGoods from '@/pages/GameGoods/GameGoodsList'
import GameGoodsDetail from '@/pages/GameGoods/GameGoodsDetail'
import Cart from '@/pages/Cart'
import CreateOrderStep1 from '@/pages/Order/CreateOrderStep1'
import CreateOrderStep2 from '@/pages/Order/CreateOrderStep2'
import CreateOrderStep3 from '@/pages/Order/CreateOrderStep3'
import MyOrder from '@/pages/Order/MyOrder'
import PayOrder from '@/pages/Order/PayOrder'
import UpdateUser from '@/pages/User/UpdateUser'
import My from '@/pages/User/My'
import UserRecharge from '@/pages/User/UserRecharge'
import ResetPassword from '@/pages/User/ResetPassword'
import GameGoodsAdmin from '@/pages/GameGoods/GameGoodsAdmin'
import OrderList from '@/pages/Order/OrderList'
import Refund from '@/pages/Order/Refund'
// 配置路由
const router = new Router({
    // 配置路由
    // 这个不设置默认会带#的url
    mode: 'history',
    routes: [
        {
            path: "/home",
            component: Home
        },
        {
            path: "/login",
            component: Login,
            meta: { noneAuthentication: true }
        },
        {
            path: "/register",
            component: Register,
            meta: { noneAuthentication: true }
        },
        {
            path: "/gameNews",
            component: GameNews
        },
        {
            path: "/gameNewsDetail/:gameNewsId?",
            component: GameNewsDetail
        },
        {
            path: "/gameGoods",
            component: GameGoods
        },
        {
            path: "/gameGoodsDetail/:gameGoodsId?",
            component: GameGoodsDetail
        },
        {
            path: "/cart",
            component: Cart,
            meta: { requireAuthentication: true },
        },
        {
            path: "/createOrderStep1",
            component: CreateOrderStep1,
            meta: { requireAuthentication: true },
        },
        {
            path: "/createOrderStep2",
            component: CreateOrderStep2,
            meta: { requireAuthentication: true },
        },
        {
            path: "/createOrderStep3",
            component: CreateOrderStep3,
            meta: { requireAuthentication: true },
        },
        {
            path: "/myOrder",
            component: MyOrder,
            meta: { requireAuthentication: true },
        },
        {
            path: "/payOrder",
            component: PayOrder,
            meta: { requireAuthentication: true },
        },
        {
            path: "/updateUser",
            component: UpdateUser,
            meta: { requireAuthentication: true },
        },
        {
            path: "/userInfo",
            component: My,
            meta: { requireAuthentication: true },
        },
        {
            path: "/userRecharge",
            component: UserRecharge,
            meta: { requireAdministrator: true },
        },
        {
            path: "/resetPassword",
            component: ResetPassword,
            meta: { requireAdministrator: true },
        },
        {
            path: "/gameGoodsAdmin",
            component: GameGoodsAdmin,
            meta: { requireAdministrator: true },
        },
        {
            path: "/orderList",
            component: OrderList,
            meta: { requireAdministrator: true },
        },
        {
            path: "/refund",
            component: Refund,
            meta: { requireAdministrator: true },
        },
        // 重定向，在项目跑起来的时候，访问/，立马让他定向到首页
        {
            path: "*",
            redirect: "/home"
        }
    ],
})

/**
 * 全局路由拦截器
 * 用于实现在路由元数据中配置了需要认证的页面，如未检测到授权信息（默认为JWT令牌），或授权超时，着转向到登陆页面，登陆后跳转回原路由页面继续操作
 */
router.beforeEach((to, from, next) => {
    //to:去的那个路由的信息
    //from:从那个路由而来的信息
    //next:放行函数!!!!!! 
    if (to.meta.requireAuthentication && !store.getters['isAuthorized']) {
        //未登陆，则直接转到登陆
        router.push('/login?redirect=' + to.path);
        //这个不知道为啥报错，应该是没catch到异常
        //next({ path: '/login', query: { redirect: to.fullPath } })
    } else {
        //已经登录，还想去登录和注册页面，立即返回前一个页面
        if (to.meta.noneAuthentication && store.getters['isAuthorized']) {
            //console.log(from.path)
            history.go(-1)
            return;
        }
        // 已经登录，但不是管理员，则转到首页
        if (to.meta.requireAdministrator && !store.getters['isAdministrator']) {
            next({path: '/'})
            return;
        } 
        next()
    }
    
})

export default router;